//
//  ViewController.m
//  5005-计算器
//
//  Created by student on 16/3/19.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


@property (weak, nonatomic) IBOutlet UILabel *label;
//用来登记有没有计算符号的输入
@property (nonatomic,assign) BOOL symbel;
//储存计算符号
@property (nonatomic,copy) NSString *str;
//储存被计算数
@property (nonatomic,assign) double precedNum;
//储存计算数
@property (nonatomic,assign) double nowNum;
//判读有没有输入等于号
@property (nonatomic,assign) BOOL equal;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.symbel = 0;
    self.precedNum = 0;
    self.nowNum = 0;
    self.equal = NO;
}
#pragma mark - 数字按键的执行代码
- (IBAction)preZero:(UIButton *)sender {
    [self symbelChanged];
    if (![self.label.text isEqualToString:@"0"]) {
        [self setUpNumBtn:@"0"];
    }
    
}

- (IBAction)preOne:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"1"];
    
}

- (IBAction)preTwo:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"2"];
}

- (IBAction)preThree:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"3"];
}

- (IBAction)preFour:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"4"];
}

- (IBAction)preFive:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"5"];
}

- (IBAction)preSix:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"6"];
    
}

- (IBAction)preSeven:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"7"];
}

- (IBAction)preEight:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"8"];
}

- (IBAction)preNine:(UIButton *)sender {
    [self symbelChanged];
    [self setUpNumBtn:@"9"];
}
#pragma mark - 清空按键
//清空的事件
- (IBAction)preClear:(UIButton *)sender {
    self.label.text = @"0";
    [self setLabelFont];
}
#pragma mark - 小数点按键
//小数点的事件
- (IBAction)prePoint:(id)sender {
    
    NSMutableString *mutStr = [NSMutableString stringWithString:self.label.text];
    NSRange range = [mutStr rangeOfString:@"."];
    if (range.location == NSNotFound) {
        [mutStr appendString:@"."];
        self.label.text = mutStr;
    }
}
#pragma mark - 正负号
// 正负号的事件
- (IBAction)preSign:(UIButton *)sender {
    NSMutableString *mutStr = [NSMutableString stringWithString:self.label.text];
    NSRange range = [mutStr rangeOfString:@"-"];
    if (range.location == NSNotFound ) {
        [mutStr insertString:@"-" atIndex:0];
        self.label.text = mutStr;
    }
    else {
        NSString *str = [mutStr substringFromIndex:1];
        self.label.text = str;
    }
}
#pragma mark - 百分号
- (IBAction)prePresent:(UIButton *)sender {
    
}
#pragma mark - 运算符号
- (IBAction)preDiv:(UIButton *)sender {
    self.symbel  = YES;
    //被操作数
    self.precedNum = [self getNum];
    self.str = @"/";
}

- (IBAction)preMul:(UIButton *)sender {
    self.symbel = YES;
    //被操作数
    self.precedNum = [self getNum];
    self.str = @"*";
}

- (IBAction)preSub:(UIButton *)sender {
    self.symbel = YES;
    //被操作数
    self.precedNum = [self getNum];
    self.str = @"-";
}

- (IBAction)preAdd:(UIButton *)sender {
    self.symbel = YES;
    //被操作数
    self.precedNum = [self getNum];
    self.str = @"+";
}
#pragma mark - 等于号
- (IBAction)preEqual:(UIButton *)sender {
    if ([self.str isEqualToString:@"/"]) {
        self.nowNum = [self getNum];
        double m = self.precedNum / self.nowNum;
        //[self calculate:m];
        //判断结果如果是整数
        if (m == (int)m) {
            int s = (int)m;
            NSString *str = [NSString stringWithFormat:@"%i",s];
            //插入逗号
            if (str.length > 3) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 3];
                if (mutStr1.length > 7) {
                    [mutStr1 insertString:@"," atIndex:mutStr1.length - 7];
                }
                self.label.text = mutStr1;
            }
            else{
                self.label.text = str;
            }
        }
        //判断结果如果是小数的情况
        else{
            NSMutableString *mutStr1 = [NSMutableString stringWithFormat:@"%f",m];
            NSRange range = [mutStr1 rangeOfString:@"."];
            //小数点前面部分
            NSString *str2 = [mutStr1 substringToIndex:range.location+1];
            NSMutableString *mutStr2 = [NSMutableString stringWithString:str2];
            //小数点后面部分
            NSString *str3 = [mutStr1 substringFromIndex:range.location+1];
            //小数点后部分的最后一个字
            NSString *str4 = [str3 substringFromIndex:str3.length - 1];
            while ([str4 isEqualToString:@"0"]) {
                NSMutableString *mutStr3 = [NSMutableString stringWithString:str3];
                str3 = [mutStr3 substringToIndex:mutStr3.length - 1];
                str4 = [str3 substringFromIndex:str3.length - 1];
            }
            //插入逗号
            if (str2.length > 4) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str2];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 4];
                NSMutableString *mutStr2 = [NSMutableString stringWithString:mutStr1];
                [mutStr1 appendString:str3];
                NSString *result = [mutStr1 substringToIndex:12];
                self.label.text = result;
                if (str2.length > 7) {
                    [mutStr2 insertString:@"," atIndex:mutStr2.length - 8];
                
                [mutStr2 appendString:str3];
                result = [mutStr2 substringToIndex:12];
                self.label.text = result;
                }
            }
            else{
                [mutStr2 appendString:str3];
                self.label.text = mutStr2;
            }
        }
        [self setLabelFont];
        self.equal = YES;
    }
    else if([self.str isEqualToString:@"+"]){
        self.nowNum = [self getNum];
        double m = self.precedNum + self.nowNum;
        //[self calculate:m];
        //判断结果如果是整数
        if (m == (int)m) {
            int s = (int)m;
            NSString *str = [NSString stringWithFormat:@"%i",s];
            //插入逗号
            if (str.length > 3) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 3];
                if (mutStr1.length > 7) {
                    [mutStr1 insertString:@"," atIndex:mutStr1.length - 7];
                }
                self.label.text = mutStr1;
            }
            else{
                self.label.text = str;
            }
        }
        //判断结果如果是小数的情况
        else{
            NSMutableString *mutStr1 = [NSMutableString stringWithFormat:@"%f",m];
            NSRange range = [mutStr1 rangeOfString:@"."];
            //小数点前面部分
            NSString *str2 = [mutStr1 substringToIndex:range.location+1];
            NSMutableString *mutStr2 = [NSMutableString stringWithString:str2];
            //小数点后面部分
            NSString *str3 = [mutStr1 substringFromIndex:range.location+1];
            //小数点后部分的最后一个字
            NSString *str4 = [str3 substringFromIndex:str3.length - 1];
            while ([str4 isEqualToString:@"0"]) {
                NSMutableString *mutStr3 = [NSMutableString stringWithString:str3];
                str3 = [mutStr3 substringToIndex:mutStr3.length - 1];
                str4 = [str3 substringFromIndex:str3.length - 1];
            }
            //插入逗号
            if (str2.length > 4) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str2];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 4];
                NSMutableString *mutStr2 = [NSMutableString stringWithString:mutStr1];
                [mutStr1 appendString:str3];
                NSString *result = [mutStr1 substringToIndex:12];
                self.label.text = result;
                if (str2.length > 7) {
                    [mutStr2 insertString:@"," atIndex:mutStr2.length - 8];
                    
                    [mutStr2 appendString:str3];
                    result = [mutStr2 substringToIndex:12];
                    self.label.text = result;
                }
            }
            else{
                [mutStr2 appendString:str3];
                self.label.text = mutStr2;
            }
        }
        [self setLabelFont];
        self.equal = YES;
    }
    else if([self.str isEqualToString:@"*"]){
        self.nowNum = [self getNum];
        double m = self.precedNum * self.nowNum;
        //[self calculate:m];
        //判断结果如果是整数
        if (m == (int)m) {
            int s = (int)m;
            NSString *str = [NSString stringWithFormat:@"%i",s];
            //插入逗号
            if (str.length > 3) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 3];
                if (mutStr1.length > 7) {
                    [mutStr1 insertString:@"," atIndex:mutStr1.length - 7];
                }
                self.label.text = mutStr1;
            }
            else{
                self.label.text = str;
            }
        }
        //判断结果如果是小数的情况
        else{
            NSMutableString *mutStr1 = [NSMutableString stringWithFormat:@"%f",m];
            NSRange range = [mutStr1 rangeOfString:@"."];
            //小数点前面部分
            NSString *str2 = [mutStr1 substringToIndex:range.location+1];
            NSMutableString *mutStr2 = [NSMutableString stringWithString:str2];
            //小数点后面部分
            NSString *str3 = [mutStr1 substringFromIndex:range.location+1];
            //小数点后部分的最后一个字
            NSString *str4 = [str3 substringFromIndex:str3.length - 1];
            while ([str4 isEqualToString:@"0"]) {
                NSMutableString *mutStr3 = [NSMutableString stringWithString:str3];
                str3 = [mutStr3 substringToIndex:mutStr3.length - 1];
                str4 = [str3 substringFromIndex:str3.length - 1];
            }
            //插入逗号
            if (str2.length > 4) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str2];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 4];
                NSMutableString *mutStr2 = [NSMutableString stringWithString:mutStr1];
                [mutStr1 appendString:str3];
                NSString *result = [mutStr1 substringToIndex:12];
                self.label.text = result;
                if (str2.length > 7) {
                    [mutStr2 insertString:@"," atIndex:mutStr2.length - 8];
                    
                    [mutStr2 appendString:str3];
                    result = [mutStr2 substringToIndex:12];
                    self.label.text = result;
                }
            }
            else{
                [mutStr2 appendString:str3];
                self.label.text = mutStr2;
            }
        }
        [self setLabelFont];
        self.equal = YES;

    }
    else if([self.str isEqualToString:@"-"]){
        self.nowNum = [self getNum];
        double m = self.precedNum - self.nowNum;
        //[self calculate:m];
        //判断结果如果是整数
        if (m == (int)m) {
            int s = (int)m;
            NSString *str = [NSString stringWithFormat:@"%i",s];
            //插入逗号
            if (str.length > 3) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 3];
                if (mutStr1.length > 7) {
                    [mutStr1 insertString:@"," atIndex:mutStr1.length - 7];
                }
                self.label.text = mutStr1;
            }
            else{
                self.label.text = str;
            }
        }
        //判断结果如果是小数的情况
        else{
            NSMutableString *mutStr1 = [NSMutableString stringWithFormat:@"%f",m];
            NSRange range = [mutStr1 rangeOfString:@"."];
            //小数点前面部分
            NSString *str2 = [mutStr1 substringToIndex:range.location+1];
            NSMutableString *mutStr2 = [NSMutableString stringWithString:str2];
            //小数点后面部分
            NSString *str3 = [mutStr1 substringFromIndex:range.location+1];
            //小数点后部分的最后一个字
            NSString *str4 = [str3 substringFromIndex:str3.length - 1];
            while ([str4 isEqualToString:@"0"]) {
                NSMutableString *mutStr3 = [NSMutableString stringWithString:str3];
                str3 = [mutStr3 substringToIndex:mutStr3.length - 1];
                str4 = [str3 substringFromIndex:str3.length - 1];
            }
            //插入逗号
            if (str2.length > 4) {
                NSMutableString *mutStr1 = [NSMutableString stringWithString:str2];
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 4];
                NSMutableString *mutStr2 = [NSMutableString stringWithString:mutStr1];
                [mutStr1 appendString:str3];
                NSString *result = [mutStr1 substringToIndex:12];
                self.label.text = result;
                if (str2.length > 7) {
                    [mutStr2 insertString:@"," atIndex:mutStr2.length - 8];
                    
                    [mutStr2 appendString:str3];
                    result = [mutStr2 substringToIndex:12];
                    self.label.text = result;
                }
            }
            else{
                [mutStr2 appendString:str3];
                self.label.text = mutStr2;
            }
        }
        [self setLabelFont];
        self.equal = YES;

    }
    
}
#pragma mark - 封装的方法
//label的字符串转换成数字
-(double)getNum{
    NSMutableString *mutStr = [NSMutableString stringWithString:self.label.text];
    NSRange range = [mutStr rangeOfString:@","];
    while (range.location != NSNotFound) {
        [mutStr deleteCharactersInRange:range];
        range = [mutStr rangeOfString:@","];
    }
    double s = [mutStr doubleValue];
    return s;
}

//监测到符号发生变化后的执行代码
-(void)symbelChanged{
    if (self.symbel == YES) {
        self.label.text = @"0";
        self.symbel = NO;
    }
}

//设置label字体大小
-(void)setLabelFont{
    if (self.label.text.length == 7) {
        [self.label setFont:[UIFont systemFontOfSize:88]];
    }
    else if(self.label.text.length == 8){
        [self.label setFont:[UIFont systemFontOfSize:78]];
    }
    else if(self.label.text.length == 9){
        [self.label setFont:[UIFont systemFontOfSize:68]];
    }
    else if(self.label.text.length == 10){
        [self.label setFont:[UIFont systemFontOfSize:62]];
    }
    else if(self.label.text.length == 11){
        [self.label setFont:[UIFont systemFontOfSize:55]];
    }
    else if(self.label.text.length == 12){
        [self.label setFont:[UIFont systemFontOfSize:55]];
    }
    else{
        [self.label setFont:[UIFont systemFontOfSize:102]];
    }
}

//按数字按钮的方法;
-(void)setUpNumBtn:(NSString *)str{
    NSMutableString *mutStr = [NSMutableString stringWithString:self.label.text];
    NSRange range2 = [mutStr rangeOfString:@"."];
    int i = 9;
    //如有小数点，那么后面的就不用判断有多少位。既“,”一直保持那个位置
    if (range2.location != NSNotFound) {
        i += 1;
        //保证有9个数字
        NSRange range = [mutStr rangeOfString:@","];
        if(range.location != NSNotFound) {
            i += 1;
            NSString *str1 = mutStr;
            NSMutableString *str2 = [NSMutableString stringWithString:str1];
            [str2 deleteCharactersInRange:range];
            range = [str2 rangeOfString:@","];
            if (range.location != NSNotFound) {
                i += 1;
            }
        }
    }
    //如果没有小数点。既","的位置需要重新排布
    else{
        NSRange range = [mutStr rangeOfString:@","];
        while (range.location != NSNotFound) {
            [mutStr deleteCharactersInRange:range];
            range = [mutStr rangeOfString:@","];
        }
    }
    if (mutStr.length < i) {
        [mutStr appendString:str];
        
        NSString *str1 = [mutStr substringToIndex:1];
        NSString *sstr = [[mutStr substringFromIndex:1] substringToIndex:1];
        //第一位是0的话就把0截掉;
        if ([str1 isEqualToString:@"0"] && ![sstr isEqualToString:@"."])
        {
            NSString *str2 = [mutStr substringFromIndex:1];
            self.label.text = str2;
        }
        else
        {
            self.label.text = mutStr;
        }
        //每三位整数添加一个逗号
        float m = [mutStr floatValue];
        if (m/1000 >= 1) {
            NSMutableString *mutStr = [NSMutableString stringWithString:self.label.text];
            [mutStr insertString:@"," atIndex:mutStr.length-3];
            self.label.text = mutStr;
        }
        if (m/1000000 >= 1) {
            NSMutableString *mutStr = [NSMutableString stringWithString:self.label.text];
            [mutStr insertString:@"," atIndex:mutStr.length-7];
            self.label.text = mutStr;
        }
    }
    [self setLabelFont];
}

//计算以及结果显示到界面上
-(void)calculate:(double)m{
    //判断结果如果是整数
    if (m == (int)m) {
        int s = (int)m;
        NSString *str = [NSString stringWithFormat:@"%i",s];
        //插入逗号
        if (str.length > 3) {
            NSMutableString *mutStr1 = [NSMutableString stringWithString:str];
            [mutStr1 insertString:@"," atIndex:mutStr1.length - 3];
            if (mutStr1.length > 7) {
                [mutStr1 insertString:@"," atIndex:mutStr1.length - 7];
            }
            self.label.text = mutStr1;
        }
        else{
            self.label.text = str;
        }
    }
    //判断结果如果是小数的情况
    else{
        NSMutableString *mutStr1 = [NSMutableString stringWithFormat:@"%f",m];
        NSRange range = [mutStr1 rangeOfString:@"."];
        //小数点前面部分
        NSString *str2 = [mutStr1 substringToIndex:range.location+1];
        NSMutableString *mutStr2 = [NSMutableString stringWithString:str2];
        //小数点后面部分
        NSString *str3 = [mutStr1 substringFromIndex:range.location+1];
        //小数点后部分的最后一个字
        NSString *str4 = [str3 substringFromIndex:str3.length - 1];
        while ([str4 isEqualToString:@"0"]) {
            NSMutableString *mutStr3 = [NSMutableString stringWithString:str3];
            str3 = [mutStr3 substringToIndex:mutStr3.length - 1];
            str4 = [str3 substringFromIndex:str3.length - 1];
        }
        //插入逗号
        if (str2.length > 4) {
            NSMutableString *mutStr1 = [NSMutableString stringWithString:str2];
            [mutStr1 insertString:@"," atIndex:mutStr1.length - 4];
            NSMutableString *mutStr2 = [NSMutableString stringWithString:mutStr1];
            [mutStr1 appendString:str3];
            NSString *result = [mutStr1 substringToIndex:12];
            self.label.text = result;
            if (str2.length > 7) {
                [mutStr2 insertString:@"," atIndex:mutStr2.length - 8];
                
                [mutStr2 appendString:str3];
                result = [mutStr2 substringToIndex:12];
                self.label.text = result;
            }
        }
        else{
            [mutStr2 appendString:str3];
            self.label.text = mutStr2;
        }
    }
    [self setLabelFont];
    self.equal = YES;
}

@end















